/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controller.Actions;

import controller.Forms.SearchEditUserForm;
import dao.UserDAO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Erro;
import model.User;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Adriano
 */
public class SearchEditUserAction extends Action    {
    
     @Override
    public ActionForward execute(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        try {

            SearchEditUserForm buf = (SearchEditUserForm) form;
            User u = (User) request.getSession().getAttribute("User");
            if(buf.getTxtEmail().isEmpty()) {
                System.err.println("BUSCAUSERACTION ");
                return mapping.findForward("erroAlias");
            }
            
            if (u == null) {
                request.getSession().setAttribute("ErroMSG", Erro.erroMsg[Erro.NAO_AUTENTICADO]);
                return mapping.findForward("erroAlias");
            } else {
                UserDAO uDao = new UserDAO();
                u = (User) uDao.find(buf.getTxtEmail());

                uDao.closeDAO();
                
                request.getSession().setAttribute("EditUser", u);
                return mapping.findForward("ResultUserEditAlias");

            }

        } catch (Exception e) {
            System.err.println("BUSCAUSEEDITRACTION - " + e.getMessage());
            return mapping.findForward("erroAlias");
        }
    }
}
